package com.pason.onlineshop.mainshop.functionaltest.framework;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.BrowserVersionFeatures;
import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
import com.gargoylesoftware.htmlunit.WebClient;

public class ExtendedHtmlUnitDriver extends HtmlUnitDriver {

    private List<String> collectedAlerts = new ArrayList<String>();

    public ExtendedHtmlUnitDriver() {
        super(true);
    }

    public ExtendedHtmlUnitDriver(final boolean enableJavascript) {
        super(enableJavascript);
    }

    public void captureAlerts() {
        WebClient webClient = getWebClient();
        webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    }

    public List<String> getCollectedAlerts() {
        return collectedAlerts;
    }

    public void setHeader(final String name, final String value) {
        this.getWebClient().addRequestHeader(name, value);
    }

    @Override
    protected WebClient newWebClient(final BrowserVersion bv) {
        BrowserVersionFeatures[] bvf = new BrowserVersionFeatures[1];
        bvf[0] = BrowserVersionFeatures.HTMLIFRAME_IGNORE_SELFCLOSING;
        return new WebClient(new BrowserVersion(bv.getApplicationName(), bv.getApplicationVersion(), bv.getUserAgent(),
                bv.getBrowserVersionNumeric(), bvf));
    }
}
